/*
 * FileDateComparator.java
 * 
 * Copyright (C) 2009 no.good.at.coding.
 * 
 * This file is part of the No Good Commons Library.
 * 
 * The No Good Commons Library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nogoodatcoding.commons;

import java.io.File;
import java.util.Comparator;

import org.apache.log4j.Logger;

/**
 *
 * Compares files based on their timestamp.
 * The timestamp is fetched using the FileTimes library.
 * For Windows systems, the greater of the creation time
 * and {@link java.io.File#lastModified()} time is returned.
 * In case of errors or in case of non-Windows systems,
 * the {@code lastModified} time is returned
 *
 * Can sort in descending (newest first) or ascending (oldest first) order
 * of timestamps.
 *
 * @see com.nogoodatcoding.commons.Commons#getLatestTimestamp(java.io.File)
 * 
 * @author no.good.at.coding
 *
 */
public class FileDateComparator 
        implements Comparator
{
    private static Logger log = Logger.getLogger(FileDateComparator.class);

    private boolean isDescending = true;

    /**
     *
     * Default constructor for convenience; assumes {@code sortDescending} is
     * {@code true}. Will return {@code FileDateComparator} that will sort by
     * descending order of timestamps i.e. newest files first
     *
     * @see com.nogoodatcoding.commons.FileDateComparator#FileDateComparator(boolean)
     *
     */
    public FileDateComparator()
    {
        this.isDescending = true;
    }

    /**
     * Constructor for {@code FileDateComparator}.
     *
     * @param sortDescending Indicates if the list is to be sorted in descending
     * order (newest first) or not (i.e. in descending order (oldest first)).
     *
     * @see com.nogoodatcoding.commons.FileDateComparator#FileDateComparator()
     */
    public FileDateComparator(boolean sortDescending)
    {
        this.isDescending = sortDescending;
    }

    public int compare(Object o1, Object o2)
    {
        log.debug("Comparing item1 and item2");

        if ( o1 == null | o2 == null )
        {
            log.error("Either item1 or item2 was null");
            throw new NullPointerException();
        }

        long timestamp1 = Commons.getLatestTimestamp((File)o1);
        long timestamp2 = Commons.getLatestTimestamp((File)o2);

        if ( timestamp1 < timestamp2 )
        {
            if(isDescending)
            {
                log.debug(((File)o1).getPath() + " < " + ((File)o2).getPath());
                return 1;
            }
            else
            {
                log.debug(((File)o1).getPath() + " > " + ((File)o2).getPath());
                return -1;
            }

        }
        else if ( timestamp1 > timestamp2 )
        {
            if(isDescending)
            {
                log.debug(((File)o1).getPath() + " > " + ((File)o2).getPath());
                return -1;
            }
            else
            {
                log.debug(((File)o1).getPath() + " < " + ((File)o2).getPath());
                return 1;
            }
        }
        else
        {
            log.debug(((File)o1).getPath() + " == " + ((File)o2).getPath());
            return 0;
        }
    }
}